#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<list>
using namespace std;

struct ListNode
{
    int val;
    struct ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* reverseBetween(ListNode* head, int m, int n)
{
    // write code here
    if (m == n)return head;
    ListNode* dummy = new ListNode(-1);
    dummy->next = head;
    ListNode* left = dummy;
    ListNode* right = dummy;
    int mm = m, nn = n;
    while (mm - 1)
    {
        left = left->next;
        mm--;
    }
    while (nn)
    {
        right = right->next;
        nn--;
    }
    ListNode* cur = left->next;
    ListNode* x = cur;
    left->next = nullptr;
    ListNode* linkhead = right->next;
    right->next = nullptr;

    ListNode* nextp = nullptr;
    while (cur)
    {
        ListNode* nextNode = cur->next;
        cur->next = nextp;
        nextp = cur;
        cur = nextNode;
    }
    left->next = right;
    x->next = linkhead;

    return dummy->next;
}int main()
{
    ListNode l(1);
    ListNode l2(2);
    ListNode l3(3);
    ListNode l4(4);
    ListNode l5(5);
    l.next = &l2;
    l2.next = &l3;
    l3.next = &l4;
    l4.next = &l5;


    reverseBetween(&l, 2, 4);
 	return 0;
}